﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI;
using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client;

using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Oracle;
using LMD.Lib.Controls;

public partial class UFTLog : ICPRelationshipPage
{
    public UFTData m_uftData = new UFTData();
    public EventLogData evlData = new EventLogData();
    public int m_iEventId = 0;
    public String m_sxUserRole = String.Empty;
        
    #region Properties
    public String m_sxEventId
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["eid"]))
            {
                return Request.QueryString["eid"];
            }
            else
            {
                return String.Empty;
            }
        }
    }
    #endregion Properties

    #region Event Handlers
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        Page.Title = "ICP Lintasarta: Relationship, Event Logs";
        m_iEventId = Context.ReadInt("eid");
        m_sxUserRole = ICPUser.Role;
        if (!IsPostBack && !IsCallback)
        {
            InitEventList();
        }
        
    }
    protected void cbkLog_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = HandleLog(e.Parameter);
    }
    private string HandleLog(string sxParam)
    {
        String[] sxSplitted = sxParam.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        String sxOutput = String.Empty;
        String sxEventId = sxSplitted[1];
        int iEventId = int.Parse(sxSplitted[1]);
        
        
        switch (sxSplitted[0].ToUpper())
        {
            case "LOG":
                DataTable dtExistingLog = DBQuery.EventTable.GetEventLog(iEventId);
                DataTable dtEvent = DBQuery.EventTable.GetEventDataByID(iEventId);
                String sxEventName = dtEvent.Rows[0]["CODE"].ToString();
                sxOutput = UFTManager.BuildEditor_EventLog(sxEventId, KFileHandler.UFT.EVENT_LOG, sxEventName, dtExistingLog);
                break;
            case "MORE":
                int iLogCount = Convert.ToInt32(sxSplitted[2]);
                sxOutput += "<dt>";
                sxOutput += CustomControls.BuildEditor_FileUpload(sxEventId + "_" + iLogCount.ToString(),
                    KFileHandler.UFT.EVENT_LOG, null, KControl.INPUT_ID, TextboxType.DocumentUpload);
                sxOutput += "</dt><dd>&nbsp;</dd>";
                break;
            case "SAVE":
                sxOutput = saveLog(sxSplitted);
                break;
            case "CANCEL" :
                sxOutput = cancelLog(sxSplitted);
                break;
        }

        return sxOutput;
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    private void InitEventList()
    {
        DataTable dtEventList = DBQuery.EventTable.GetClosedEvent();

        dgEventList.DataSource = dtEventList;
        dgEventList.DataBind();
    }
    private String saveLog(String[] sxSplitted)
    {
        String sxReturn = String.Empty;
        String sxEventId = sxSplitted[2];
        
        DBLib dbl = new DBLib();
        dbl.BeginTransaction();

        try
        {
            for (int i = sxSplitted.Length - (sxSplitted.Length - 2); i < sxSplitted.Length; i++)
            {
                String[] sxRowSplitted = sxSplitted[i].Split(
                    new String[] { KGlobal.NEWLINE }, StringSplitOptions.None);

                EventLogData evlData = new EventLogData();
                evlData.iEventId = Convert.ToInt32(sxSplitted[1]);
                evlData.iFileUploadId = Convert.ToInt32(sxRowSplitted[1]);
                DBQuery.EventTable.SaveEventLog(dbl, evlData);
            }


            // COMMIT 
            dbl.EndTransaction(true);
        }
        catch (Exception ex)
        {
            //ROLLBACK
            dbl.EndTransaction(false);
            throw ex;
        }

        return sxReturn;
    }
    private String cancelLog(String[] sxSplitted)
    {
        String sxReturn = String.Empty;
        String sxEventId = sxSplitted[2];

        DBLib dbl = new DBLib();
        dbl.BeginTransaction();

        try
        {
            for (int i = sxSplitted.Length - (sxSplitted.Length - 2); i < sxSplitted.Length; i++)
            {
                String[] sxRowSplitted = sxSplitted[i].Split(
                    new String[] { KGlobal.NEWLINE }, StringSplitOptions.None);
                if (sxRowSplitted.Length > 1)
                {
                    int iFileUploadId = Convert.ToInt32(sxRowSplitted[1]);
                    Object[] objFileInfo = DBQuery.FileUploadTable.GetDownloadURL(iFileUploadId);
                    String sxFileName = objFileInfo[1].ToString();
                    String sxPath = objFileInfo[2].ToString();
                    String sxFullPathAndName = MapPath(sxPath + "/" + sxFileName);
                    LMD.Lib.IO.IOLib.DeleteFile(sxFullPathAndName);
                    DBQuery.FileUploadTable.PermanentDeleteFile(iFileUploadId);
                }
                else
                {
                    break;
                }
            }


            // COMMIT 
            dbl.EndTransaction(true);
        }
        catch (Exception ex)
        {
            //ROLLBACK
            dbl.EndTransaction(false);
            throw ex;
        }

        return sxReturn;
    }
    public String GetCloseColumnContent(String id, String sxClose)
    {
        String sxOutput = String.Empty;

        if ((ICPUser.IsAdmin || ICPUser.IsAdminForum) && sxClose == "Y")
        {
            sxOutput = String.Format
                ("<a href=\"#\">Add Log</a>", id);
        }
        else
        {
            sxOutput = "-";
        }
        

        return sxOutput;
    }
    #endregion Custom Methods
}
